package com.nf.health.app.datamain.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/3/29.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 尿检测量结果展示类
 * @emial he_pann@163.com
 */
public class UrineTestMeasuredResultActivity extends BaseActivity {

    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_glucose_value)
    TextView tvGlucoseValue;
    @Bind(R.id.tv_urine_protein_value)
    TextView tvUrineProteinValue;
    @Bind(R.id.tv_bilirubin_value)
    TextView tvBilirubinValue;
    @Bind(R.id.tv_nitrite_value)
    TextView tvNitriteValue;
    @Bind(R.id.tv_scale_value)
    TextView tvScaleValue;
    @Bind(R.id.tv_red_cell_value)
    TextView tvRedCellValue;
    @Bind(R.id.tv_urobilinogen_value)
    TextView tvUrobilinogenValue;
    @Bind(R.id.tv_acetone_body_value)
    TextView tvAcetoneBodyValue;
    @Bind(R.id.tv_white_cell_value)
    TextView tvWhiteCellValue;
    @Bind(R.id.tv_ph_value)
    TextView tvPhValue;
    @Bind(R.id.et_health_tips)
    TextView etHealthTips;
    @Bind(R.id.btn_measure)
    Button btnMeasure;

    //尿检数据信息 - 数组(注意位置)
    private String[] detailResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_urine_test_measured_result);
        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "测量结果");
        btnMeasure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
//                ActivityUtils.showActivity(UrineTestMeasuredResultActivity.this, UrineTestActivity.class);
            }
        });

        String result = PreferenceHelper.getString(PreferenceHelper.URINE_TEST_MEASURED_INFO, "");
        detailResult = result.split(",");

        initData();
    }

    /**
     * 展示测量数据
     */
    private void initData() {
        if (detailResult.length == 10) {
            tvGlucoseValue.setText(detailResult[0]);         //葡萄糖
            tvBilirubinValue.setText(detailResult[1]);       //胆红素
            tvAcetoneBodyValue.setText(detailResult[2]);     //酮体
            tvScaleValue.setText(detailResult[3]);           //比重
            tvRedCellValue.setText(detailResult[4]);         //红细胞
            tvPhValue.setText(detailResult[5]);              //PH
            tvUrineProteinValue.setText(detailResult[6]);    //尿蛋白
            tvUrobilinogenValue.setText(detailResult[7]);    //尿胆原
            tvNitriteValue.setText(detailResult[8]);         //亚硝酸盐
            tvWhiteCellValue.setText(detailResult[9]);       //白细胞

            //设置健康tips
            etHealthTips.setText(getTipsString(detailResult));
            //设置时间
            tvDate.setText(DateUtil.date2HHmm(new Date()));
        }else{
            tvGlucoseValue.setText("测量结果不全");
            etHealthTips.setText("没有测量结果,暂无建议");
        }
    }

    /**
     * 根据最近测量结果设置健康贴士
     * @param detailResult 存储的十项测量结果
     * @return
     */
    private String getTipsString(String[] detailResult) {
        StringBuilder result = new StringBuilder();
        if (detailResult[0].contains("+")){//葡萄糖
            result.append(getResources().getString(R.string.putaotang_tip));
            result.append("\r\n");
        }

        if (detailResult[1].contains("+")){//胆红素
           result.append(getResources().getString(R.string.danhongsu_tip));
           result.append("\r\n");
        }

        if (detailResult[2].contains("+")){//酮体
           result.append(getResources().getString(R.string.tongti_tip));
           result.append("\r\n");
        }

        if (detailResult[4].contains("+")){//红细胞
           result.append(getResources().getString(R.string.hongxibao_tip));
           result.append("\r\n");
        }

        if (detailResult[6].contains("+")){//尿蛋白
           result.append(getResources().getString(R.string.niaodanbai_tip));
           result.append("\r\n");
        }

        if (Float.valueOf(detailResult[7]) > 1.0f){//尿胆原
           result.append( getResources().getString(R.string.niaodanyuan_tip));
           result.append("\r\n");
        }

        if (!detailResult[8].equals("-")){//亚硝酸盐
           result.append(getResources().getString(R.string.yaxiaosuanyan_tip));
           result.append("\r\n");
        }

        if (detailResult[9].contains("+")){//白细胞
           result.append(getResources().getString(R.string.baixibao_tip));
           result.append("\r\n");
        }
        if (TextUtils.isEmpty(result)){
            return "指标正常,祝君健康";
        }
        return result.toString();
    }
}
